<?php
require_once WACT_ROOT . 'view/view.inc.php';
require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';

require_once WACT_ROOT . 'validation/rules/match.inc.php';
require_once WACT_ROOT . 'validation/rules/member.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';

require_once WACT_ROOT . 'util/dataspace.inc.php';

require_once APP_ROOT . "view/pub/myprofile.view.php";
require_once APP_ROOT . "model/common/session.php";
require_once APP_ROOT . "model/common/state.php";
require_once APP_ROOT . "model/common/user.php";
require_once APP_ROOT . "model/common/site.php";


class MyprofileIndexPage extends PageController {
	function MyprofileIndexPage(){
		parent::PageController();

		$Form=new FormController();

		$Form->addChild('update', new ButtonController(new Delegate($this, 'update')));
		$Form->addChild('retrieve', new ButtonController(new Delegate($this, 'retrieve')));

		$Form->addRule(new RequiredRule('firstname'));
		$Form->addRule(new RequiredRule('lastname'));

		$Form->addRule(new RequiredRule('email'));
		$Form->addRule(new PatternRule('email','/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i'));

		//rul for zip
		$zipRule=& new PatternRule('zip','/^[0-9]{4,5}$/');
		$zipRule->setGroup('ziprule');
		$Form->addRule($zipRule);

		$Form->addRule(new PatternRule('home_phone','/^[0-9- +]{11,}$/'));
		$Form->addRule(new PatternRule('work_phone','/^[0-9- +]{11,}$/'));

		$this->addChild('myprofile_form',$Form);
		$this->setDefaultChild('myprofile_form');


        $mytemplate1 = 'pub/myprofile/message.html';


		$this->addView('messagepage',new MyprofileView($mytemplate1,2));

        $mytemplate2 = 'pub/myprofile/index.html';

        $mytemplate3 = 'pub/myprofile/notlogin.html';

		if(Session::isLogin())
			$this->setDefaultView(new MyprofileView($mytemplate2,1));
		else
			$this->setDefaultView(new MyprofileView($mytemplate3,0));

	}


	function update(&$controller, &$request, &$responseModel){
		$login=$request->getPostProperty('login');
    	$user=&User::getRecordbyLogin($login);
    	$userSes=Session::getUserArray();

    	if($user!=null&&$login!=$userSes['login']) {
    		$responseModel->set('exlogin',true);
    	}
    	else if($responseModel->isValid()){
			$responseModel->set('retrive',null);
			$id=Session::getUserId();
			User::updateUserAccountInfo($id,$responseModel);
			$user=User::getRecord($id);
			Session::setUser($user);
			$responseModel->set('message','You account information was update successfully.');
			$responseModel->set('exlogin',null);
			return 'messagepage';
		}
		$responseModel->set('retrieve',true);
	}

	function retrieve(&$controller, &$request, &$responseModel){
		$responseModel->set('exlogin',null);
		$responseModel->set('retrieve',null);

	}

}


class MyprofileChngpasswordPage extends PageController {
	function MyprofileChngpasswordPage(){
		parent::PageController();

		$Form=new FormController();
		$Form->addChild('changepassword', new ButtonController(new Delegate($this, 'changepassword')));

		$Form->addRule(new RequiredRule('oldpassword'));
		$Form->addRule(new RequiredRule('password'));
		$Form->addRule(new RequiredRule('confpswd'));
		$Form->addRule(new MatchRule('password', 'confpswd'));

		$this->addChild('myprofile_form',$Form);
		$this->setDefaultChild('myprofile_form');

        $site_id = Site::getTemplateSiteID();
        $mytemplate1 = 'pub/myprofile/message.html';


		$this->addView('messagepage',new MyprofileView($mytemplate1,2));

        $mytemplate2 = 'pub/myprofile/index.html';

        $mytemplate3 = 'pub/myprofile/notlogin.html';

		if(Session::isLogin())
			$this->setDefaultView(new MyprofileView($mytemplate2,3));
		else
			$this->setDefaultView(new MyprofileView($mytemplate3,0));

	}

	function changepassword(&$controller, &$request, &$responseModel){
		if($responseModel->isValid()){
			$id=Session::getUserId();
			$user=&User::getRecord($id);
			if($responseModel->get('oldpassword')==$user->get('password')){
				User::updateUserPassword($id,$responseModel);
				$responseModel->set('message','Your password was changed successfully.');
				return 'messagepage';
			}else $responseModel->set('passworderror','true');
		}
	}
}

?>
